package com.helpustotravel.model.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.helpustotravel.core.DAOBase;
import com.helpustotravel.model.jdo.Help;

public class HelpDAO extends DAOBase {

	public HelpDAO(PersistenceManager pm) {
		super(pm);
	}
	
	public void addHelp(Help help) {
		try {
			pm.makePersistent(help);
		} finally {
			
		}
	}
	
	@SuppressWarnings("unchecked")
	public Help getHelpBySession(String session) {
		Query query = pm.newQuery("select from " + Help.class.getName() + 
				" where session == sessionParam " + 
				"parameters String sessionParam");
		List<Help> helps = (List<Help>)query.execute(session);
		if(helps.size() > 0) {
			return helps.get(0);
		} else {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Help getHelpByPosition(String travellerName, long positionId) {
		Query query = pm.newQuery("select from " + Help.class.getName() +
				" where positionId == positionIdParam " +
				"&& travellerName == travellerNameParam " +
				"parameters String travellerNameParam, long positionIdParam");
		List<Help> helps = (List<Help>)query.execute(travellerName, positionId);
		if(helps.size() > 0) {
			return helps.get(0);
		} else {
			return null;
		}
	}

	public void updateHelpInfos(String session, String prenom, String email, String comment) {
		Help help = getHelpBySession(session);
		if(help != null) {
			help.setPrenom(prenom);
			help.setEmail(email);
			help.setComment(comment);
			try {
				pm.makePersistent(help);
			} finally {
				
			}
		}
	}

}

